
package com.jf.cloud.search.listener;

import cn.throwx.canal.gule.model.CanalBinLogResult;
import com.jf.cloud.api.product.feign.SkuFeignClient;
import com.jf.cloud.api.product.feign.SpuFeignClient;
import com.jf.cloud.common.cache.constant.CacheNames;
import com.jf.cloud.common.cache.constant.ProductCacheNames;
import com.jf.cloud.common.cache.util.RedisUtil;
import com.jf.cloud.search.bo.SkuBO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.Objects;

/**
 * @author zz
 * @date 2020/11/13
 */
@Component
public class SkuCanalListener extends BaseCanalBinlogEventThrowExceptionProcessor<SkuBO> {

    private static final Logger log = LoggerFactory.getLogger(SkuCanalListener.class);

    @Autowired
    private SpuFeignClient spuFeignClient;
    @Autowired
    private SkuFeignClient skuFeignClient;

    /**
     * 插入商品，此时插入es
     */
    @Override
    protected void processInsertInternal(CanalBinLogResult<SkuBO> result) {
        //更新前的数据
        SkuBO beforeData = result.getBeforeData();
        // 更新之后的数据
        SkuBO afterData = result.getAfterData();
        Object o = RedisUtil.get(ProductCacheNames.SKU_STATUS_DELETE + afterData.getSpuId());
        if (Objects.isNull(o)){
            //sku种类增加，商家是手动设价，下架商家的商品
            spuFeignClient.skuUpdateLaterOperate(afterData.getSpuId());
        }
        //供应商品sku增加或者删除处理代销商品sku
        spuFeignClient.supplierSkuAddOrDeleteLaterOperate(afterData.getSpuId(),afterData.getSkuId());
        spuFeignClient.changeSynUpdate(afterData.getSpuId());
    }

    /**
     * 更新商品，删除商品索引，再重新构建一个
     */
    @Override
    protected void processUpdateInternal(CanalBinLogResult<SkuBO> result) {
        //更新前的数据
        SkuBO beforeData = result.getBeforeData();
        // 更新之后的数据
        SkuBO afterData = result.getAfterData();
        //判断是否修改sku售价,当供应商修改sku售价时，商家代代销设置了手动（当供货价高于售价时自动下架商品sku）时，
        if (!Objects.isNull(afterData.getPriceFee()) && Objects.isNull(beforeData.getStatus())){
            //当代销商品sku售价低于供货价时下架代销商品
            spuFeignClient.soldOutSpu(afterData.getSkuId(),afterData.getPriceFee());
        }
        //供应商体积或重量发生变化，供应商商品是商家发货，下架商家商品
        if (!Objects.isNull(beforeData.getWeight()) || !Objects.isNull(beforeData.getVolume())){
            spuFeignClient.skuWeightOrVolumeUpdateOperate(afterData.getSpuId());
        }
        //供应商市场价变更后同步变更代销商品市场价
        if (!Objects.isNull(afterData.getMarketPriceFee())){
            skuFeignClient.synMarketPrice(afterData.getSkuId(),afterData.getMarketPriceFee());
        }
        if (Objects.equals(beforeData.getStatus(),0)){
            //sku启用
            RedisUtil.set(ProductCacheNames.SKU_STATUS_ENABLE + afterData.getSpuId(), 1, 3600);
            spuFeignClient.changeSynUpdate(afterData.getSpuId());
        }
        if (Objects.equals(beforeData.getStatus(),1)){
            //sku禁用
            RedisUtil.set(ProductCacheNames.SKU_STATUS_DISABLE + afterData.getSpuId(), 1, 3600);
            spuFeignClient.changeSynUpdate(afterData.getSpuId());
        }
        // 批量删除与sku关联的缓存信息
        RedisUtil.deleteBatch(Arrays.asList(CacheNames.SKU_WITH_ATTR_LIST_KEY +CacheNames.UNION+ afterData.getSpuId(),
                CacheNames.SKU_STOCK_LIST_KEY+CacheNames.UNION+ afterData.getSpuId(),
                CacheNames.SKU_KEY+CacheNames.UNION+ afterData.getSkuId(),
                CacheNames.SKU_STOCK_KEY + CacheNames.UNION + afterData.getSkuId(),
                ProductCacheNames.SPU_ZONE_STOCK + CacheNames.UNION + afterData.getSpuId()));
    }


}
